package com.zxg.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.taobao.common.dao.persistence.DBRoute;
import com.zxg.zbc.dal.dao.exception.DAOException;
import com.zxg.zbc.dal.query.CommonQuery;

public class CommonJDBCBaseDao<T> extends JDBCBaseDao {

  public long _insert(T item) throws DAOException {
    try {
      long id = this.insertOrUpdateNotNull(DBRoute.getAssistantRoute(), item);
      return id;
    } catch (com.taobao.common.dao.persistence.exception.DAOException e) {
      log.error("error  insert ", e);
      throw new DAOException("", e);
    }
  }

  public long _update(T item) throws DAOException {
    try {
      long num = this.update(DBRoute.getAssistantRoute(), item, true);
      return num;
    } catch (com.taobao.common.dao.persistence.exception.DAOException e) {
      log.error("error update ", e);
      throw new DAOException("error update", e);
    }
  }

  public CommonQuery<T> query(CommonQuery<T> query, String tableName, Class<T> classType) throws DAOException {
    String countSql = "select count(*) from " + tableName + " where 1=1 ";
    String selectSql = "select " + query.getSelectSql() + " from " + tableName + " where 1=1 ";
    List<Object> params = new ArrayList<Object>();
    List<Object> paramsSelect = new ArrayList<Object>();

    if (query.getParamsAnd() != null) {
      for (String key : query.getParamsAnd().keySet()) {
        Object value = query.getParamsAnd().get(key);
        countSql += " and " + key + "&" + value + "=" + value;
        selectSql += " and " + key + "&" + value + "=" + value;

      }

    }

    if (query.getParamsStart() != null) {
      for (String key : query.getParamsStart().keySet()) {
        Object value = query.getParamsStart().get(key);
        countSql += " and " + key + " >= ?";
        selectSql += " and " + key + " >= ?";
        params.add(value);
        paramsSelect.add(value);

      }

    }

    if (query.getParamsEnd() != null) {
      for (String key : query.getParamsEnd().keySet()) {
        Object value = query.getParamsEnd().get(key);
        countSql += " and " + key + " <= ?";
        selectSql += " and " + key + " <= ?";
        params.add(value);
        paramsSelect.add(value);

      }

    }

    if (query.getParamsAndNot() != null) {
      for (String key : query.getParamsAndNot().keySet()) {
        Object value = query.getParamsAndNot().get(key);
        countSql += " and " + key + "&" + value + " <> " + value;
        selectSql += " and " + key + "&" + value + " <> " + value;

      }

    }
    if (query.getParamsLike() != null) {
      for (String key : query.getParamsLike().keySet()) {
        String value = query.getParamsLike().get(key);
        countSql += " and " + key + " like '%" + value + "%'";
        selectSql += " and " + key + " like '%" + value + "%'";

      }

    }
    if (query.getParamsExceptPageParam() != null) {
      for (String key : query.getParamsExceptPageParam().keySet()) {
        Object value = query.getParamsExceptPageParam().get(key);
        countSql += " and " + key + " = ?";
        selectSql += " and " + key + " = ?";
        params.add(value);
        paramsSelect.add(value);
      }

    }

    if (query.getOrderByField() != null) {
      selectSql += " order by " + query.getOrderByField() + " " + query.getOrder();
    }
    selectSql += " limit ?,?";
    paramsSelect.add(query.getStartRow());
    paramsSelect.add(query.getPageSize());
    Object[] ps = params.toArray();
    Object[] ps2 = paramsSelect.toArray();
    try {
      Integer count = getSingleResult(DBRoute.getAssistantRoute(), Integer.class, countSql, ps);
      query.setCount(count);
      if (count > 0) {
        List<T> items = getResultList(DBRoute.getAssistantRoute(), classType, selectSql, ps2);
        query.setResults(items);
      }
    } catch (com.taobao.common.dao.persistence.exception.DAOException e) {
      log.error("error query:" + query, e);
      throw new DAOException("error query:" + query, e);
    }

    return query;
  }
}
